package com.xuce.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuce.constant.RedisConstant;
import com.xuce.dao.mapper.AdminMapper;
import com.xuce.dto.ResultBean;
import com.xuce.pojo.TUser;
import com.xuce.service.AdminService;
import com.xuce.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 徐策
 * @date 2020/3/16 19:36
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, TUser> implements AdminService {
    @Resource
    AdminMapper adminMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public TUser findOneLoginUser(TUser tUser) {
        return adminMapper.findLoginUser(tUser);
    }

    @Override
    public ResultBean checkLogin(String uname, String password) {
        TUser tUser = adminMapper.selectByUsername(uname,password);
        if (tUser!=null) {
            System.out.println(tUser);
            return ResultBean.success(tUser,"登陆成功！");
        }
        else {
            return ResultBean.error("用户名或密码错误");
        }
    }

//    @Override
//    public ResultBean checkLogin(TUser tUser) {
//         return ResultBean.success(adminMapper.findLoginUser(tUser));
//    }

    /**
     * 根据uuid组织键，去redis中查有没有相应的登录信息
     * @param uuid
     * @return
     */
    @Override
    public ResultBean checkIsLogin(String uuid) {
        if(uuid!=null&&!"".equals(uuid)){
            //1.组织键   user:login:dbe06afc-8540-4b74-8035-99e188d33933
            String redisKey = StringUtil.getRedisKey(RedisConstant.USER_LOGIN_PRE, uuid);
            //2.去redis中查
            Object o = redisTemplate.opsForValue().get(redisKey);
            if(o!=null){
                TUser user  = (TUser) o;
                user.setPassword("");//数据传递时不带密码
                return ResultBean.success(user,"用户已登录");
            }
        }
        return ResultBean.error("用户未登录");
    }
}
